package com.tfswx.jkgl.core.api;

import com.tfswx.jkgl.core.bean.JkglRuntimeData;
import com.tfswx.jkgl.core.bean.MyMultipartFile;
import com.tfswx.jkgl.core.exception.AppException;
import com.tfswx.jkgl.core.service.ApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ricky
 * @date 2021/12/13
 */
@Api(tags = "接口调用控制器")
@RestController
// @RequestMapping("")
public class ApiController {

    @Resource
    private ApiService apiService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private JkglRuntimeData jkglRuntimeData;

    @ResponseBody
    @ApiOperation("使用jkdz请求")
    @GetMapping("api/{mkbs}/{jkmc}")
    public Object jkdzQqGet(@PathVariable String mkbs, @PathVariable String jkmc, @RequestBody Object inputDTO) throws IOException {
        Map<String, String> jkdzMap = jkglRuntimeData.getJkdzMap();
        if (jkdzMap == null) {
            throw new RuntimeException("未加载接口数据【jkdzMap】");
        }
        String jkdz = "/" + mkbs + "/" + jkmc;
        String jkbm = jkdzMap.get(jkdz);
        if (StringUtils.isBlank(jkbm)) {
            throw new RuntimeException("接口【" + jkdz + "】未找到");
        }
        // String id = IdUtil.fastSimpleUUID();
        // FileUtils.writeStringToFile(new File("jkxx", jkbm + "_" + id + ".json"), JSON.toJSONString(inputDTO), StandardCharsets.UTF_8);
        // response.setHeader("id", id);
        return apiService.excute(jkbm, inputDTO);
    }

    @ResponseBody
    @ApiOperation("使用jkdz请求")
    @PostMapping("api/{mkbs}/{jkmc}")
    public Object jkdzQqPost(@PathVariable String mkbs, @PathVariable String jkmc, @RequestBody Object inputDTO) throws IOException {
        return jkdzQqGet(mkbs, jkmc, inputDTO);
    }

    @ResponseBody
    @ApiOperation("Post接口")
    @PostMapping("api/{jkbm}")
    public Object apiPostJson(@PathVariable String jkbm, @RequestBody Object inputDTO) throws IOException {
        // String id = IdUtil.fastSimpleUUID();
        // FileUtils.writeStringToFile(new File("jkxx", jkbm + "_" + id + ".json"), JSON.toJSONString(inputDTO), StandardCharsets.UTF_8);
        // response.setHeader("id", id);
        return apiService.excute(jkbm, inputDTO);
    }

    @ResponseBody
    @ApiOperation("文件上传相关接口")
    @PostMapping("fpi/{jkbm}")
    public Object apiPost(@PathVariable String jkbm) throws ServletException, IOException {

        Map<String, Object> inputDTO = new HashMap<>();
        for (Part part : request.getParts()) {
            if (part.getSize() == -1) {
                continue;
            }
            if (part.getContentType() == null) {
                continue;
            }
            String name = part.getName();
            MultipartFile multipartFile = new MyMultipartFile(part.getName(), part.getSubmittedFileName(), part.getContentType(), part.getInputStream());
            inputDTO.put(name, multipartFile);
        }
        return apiService.excute(jkbm, inputDTO);
    }

    @ResponseBody
    @ApiOperation("Get接口")
    @GetMapping("api/{jkbm}")
    public Object apiGet(@PathVariable String jkbm, Object inputDTO) {
        return apiService.excute(jkbm, inputDTO);
    }
}
